<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use app\cash\model\OrderDetails;
use app\cash\model\StoreGoodsLog;
class Index extends Controller{
    /**
     * @Author lignyun
     * @Desc    检查登录
     * 2018-04-09
     */
    public function _initialize(){
        if(!session('cash_user')) {
            $this->redirect('@cash/login/index');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    首页
     * @return mixed
     */
    public function index(){
        $user = session('cash_user');
        $store = db('store')->where('id',$user['store_id'])->find();
        $goods_category = db('goods_category')->where(['is_exp'=>0,'status'=>1,'is_deleted'=>0])->limit(5)->select();

        $this->assign('count_category',count($goods_category));
        $this->assign('cat_id',$goods_category[0]['id']);
        $this->assign('goods_category',$goods_category);
        $this->assign('store', $store);
        $this->assign('user', $user);
        $this->assign('title', '收银台');

        return view();
    }

    /**
     * @Author  lingyun
     * @Desc    获取会员信息
     * 2018-06-06
     */
    public function get_member(){
        $key = input('mobile');         //会员信息
        $store_id = session('cash_user')['store_id'];

        if($key == ''){
            $this->error('请输入会员手机号或会员卡号','');
        }

        $member = db('user')->where(['mobile'=>$key,'is_deleted'=>0])->find();
        if(!empty($member)){
            $member_level = db('sys_level')->where('id',$member['level_id'])->value('title');
            $coupon_count = db('user_ycard')->where(['type'=>['neq',2],'uid'=>$member['uid'],'use_start_time'=>['lt',time()],'use_end_time'=>['gt',time()],'store_id'=>$store_id])->count();
            $logo = empty($member['logo'])?'/static/index/touxiang.png':$member['logo'];
            $data = [
                'uid'=>$member['uid'],
                'realname'=>$member['realname'],
                'level_title'=>$member_level,
                'coupon_count'=>$coupon_count,
                'pay_code'=>$member['pay_code'],
                'mobile'=>$member['mobile'],
                'user_money'=>$member['user_money'],
                'pay_points'=>$member['pay_points'],
                'logo'=>$logo,
            ];
            $this->success('查询成功','',$data);
        }else{
            $member = db('user')->where(['pay_code'=>$key,'is_deleted'=>0])->find();
            if(!empty($member)){
                $member_level = db('sys_level')->where('id',$member['level_id'])->value('title');
                $coupon_count = db('user_ycard')->where(['type'=>['neq',2],'uid'=>$member['uid'],'use_start_time'=>['lt',time()],'use_end_time'=>['gt',time()],'is_used'=>0,'store_id'=>$store_id])->count();
                $logo = empty($member['logo'])?'/static/index/touxiang.png':$member['logo'];
                $data = [
                    'uid'=>$member['uid'],
                    'realname'=>$member['realname'],
                    'level_title'=>$member_level,
                    'coupon_count'=>$coupon_count,
                    'pay_code'=>$member['pay_code'],
                    'mobile'=>$member['mobile'],
                    'user_money'=>$member['user_money'],
                    'pay_points'=>$member['pay_points'],
                    'logo'=>$logo,
                ];
                $this->success('查询成功','',$data);
            }
        }
        $this->error('找不会员信息，请重试','');
    }

    /**
     * @Author  lingyun
     * @Desc    获取商品属性信息
     */
    public function get_goods_specs(){
        $id = input('id');
        $is_shuxing = 1;
        $store_id = session('cash_user')['store_id'];
        $goods_spec_detail_id = db('store_goods_stock')->where(['store_id'=>$store_id,'goods_id'=>$id])->column('goods_spec_id');      //商品属性id

        $spec_id = db('goods_spec_detail')->where('id','in',$goods_spec_detail_id)->column('specs');
        $spec_id = implode(',',$spec_id);
        $spec_id = explode(',',$spec_id);
        $spec_id = array_unique($spec_id);     //去重


        $spec_name = db('goods_spec')->alias('a')->join('goods_spec_name b','a.spec_id=b.id','LEFT')->where('a.id','in',$spec_id)->column('b.id,b.name');


        $spec_name_id = array_keys($spec_name);
        $spec_title = (array)array_unique($spec_name);     //去重
        $spec_title = array_values($spec_title);

        if(empty($spec_title)){
            $is_shuxing = 0;
            $spec_title = [];
            array_push($spec_title,'选择属性');
        }

        if(empty($spec_name_id)){
            $spec_detail = [];
            $spec_detail[0][0]['id'] = 0;
            $spec_detail[0][0]['detail'] = '无属性';
        }

        if(!empty($spec_name_id)){
            foreach($spec_name_id as $k => $v){
                $map = [
                    'id'=>['in',$spec_id],
                    'spec_id'=>['eq',$v]
                ];
                $result = db('goods_spec')->where($map)->field('id,detail')->select();       //属性详情

                if(empty($result)){
                    $spec_detail[0][0]['id'] = 0;
                    $spec_detail[0][0]['detail'] = '无属性';
                }
                $spec_detail[$k] = $result;
            }
        }

        //返回查询的数据
        $data = [
            'spec_title'=>$spec_title,
            'spec_detail'=>$spec_detail,
            'goods_id'=>$id,
            'is_shuxing'=>$is_shuxing
        ];

//        halt($data);
        $this->success('查询成功','',$data);
    }

    /**
     * @Author  lingyun
     * @Desc    根据商品分类找到商品信息
     * 2018-05-08
     */
    public function get_goods(){
        $cat_id = input('cat_id');
        $user = session('cash_user');
        $store = db('store')->where('id',$user['store_id'])->find();
        $join = [
            ['ys_goods g', 'a.goods_id=g.id', 'LEFT'],
        ];
//        $goods = db('store_goods_stock')->alias('a')->join($join)->where(['a.store_id'=>$user['store_id'],'g.cat_id'=>$cat_id])->field('a.is_diannei,g.id,g.name,g.price,g.member_price')->where(['a.onsale'=>1,'a.is_deleted'=>0])->order('g.price asc')->group('a.id')->select();
//,case when LENGTH(b.name) < 7 then b.name else CONCAT(lef(b.name,6),'...') end b.name
        
        $sql = "select case when LENGTH(b.name) < 20 then b.name else CONCAT(left(b.name,20),'...') end goods_name, a.is_diannei,b.id,(0+b.price) as price,b.member_price from ys_store_goods_stock as a left join ys_goods as b on a.goods_id=b.id where a.store_id=".$user['store_id']." and b.cat_id=".$cat_id." and a.onsale=1 and a.is_deleted=0 group by b.id order by price asc,id desc  ";
        $goods = Db::query($sql);

        if(!empty($goods)){
            $this->success('找到商品','',$goods);
        }else{
            $this->error('找不到商品信息','','');
        }
    }

    /**
     * 退出登录
     */
    public function out(){
        session('cash_user', null);
        //@lingyun   2018-04-09  防止销毁session  start
//        session_destroy();
        //@lingyun   2018-04-09  防止销毁session  end
        $this->success('退出登录成功！', '@cash/login');
    }

}